﻿/*
*VMukti -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, VMukti Solutions Pvt. Ltd.
*
* Hardik Sanghvi <hardik@vmukti.com>
*
* See http://www.vmukti.com for more information about
* the VMukti project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using VMukti.Presentation.MainPod;
using VMuktiAPI;

namespace VMukti.Presentation.Page_Tab
{
    public class ClsTabsTabItem : TabItem,IDisposable
    {
        string strTabId;
        string strTabTitle;
        string strTabTag;
        string[] strServerUri;
        List<string> lstBuddylist = new List<string>();

        public Button btnClose;
        public Button btnRename;
        public Button btnDelete;
        public ComboBox cmbTabBuddy;

        //public static IPodResizeChannel channelPodResize;
        //NetPeerClient npcPodResize;
        //object objPodResize = new ClsPodResize();

        public string TabID
        {
            get
            {
                return strTabId;
            }
            set
            {
                strTabId = value;
            }
        }

        public string TabTitle
        {
            get
            {
                return strTabTitle;
            }
            set
            {
                strTabTitle = value;
            }
        }

        public string TabTag
        {
            get
            {
                return strTabTag;
            }
            set
            {
                strTabTag = value;
            }
        }

        public string[] ServerUri
        {
            get
            {
                return strServerUri;
            }
            set
            {
                strServerUri = value;
            }
        }

        public List<string> lstBuddyDragged
        {
            get
            {
                return lstBuddylist;
            }
            set
            {
                lstBuddylist = value;
            }
        }

        static ClsTabsTabItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ClsTabsTabItem), new FrameworkPropertyMetadata(typeof(ClsTabsTabItem)));

        }

        public static readonly RoutedEvent CloseTab = EventManager.RegisterRoutedEvent("CloseTabEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ClsTabsTabItem));
        public static readonly RoutedEvent RenameTab = EventManager.RegisterRoutedEvent("RenameTabEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ClsTabsTabItem));
        public static readonly RoutedEvent DeleteTab = EventManager.RegisterRoutedEvent("DeleteTabEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ClsTabsTabItem));
        public static readonly RoutedEvent TabBuddyEnter = EventManager.RegisterRoutedEvent("TabBuddyEnterEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ClsTabsTabItem));
        public static readonly RoutedEvent TabBuddyLeave = EventManager.RegisterRoutedEvent("TabBuddyLeaveEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ClsTabsTabItem));


        public event RoutedEventHandler CloseTabEvent
        {
            add
            {
                AddHandler(CloseTab, value);
            }
            remove
            {
                RemoveHandler(CloseTab, value);
            }
        }

        public event RoutedEventHandler RenameTabEvent
        {
            add
            {
                AddHandler(RenameTab, value);
            }
            remove
            {
                RemoveHandler(RenameTab, value);
            }
        }

        public event RoutedEventHandler DeleteTabEvent
        {
            add
            {
                AddHandler(DeleteTab, value);
            }
            remove
            {
                RemoveHandler(DeleteTab, value);
            }
        }

        public event RoutedEventHandler TabBuddyEnterEvent
        {
            add
            {
                AddHandler(TabBuddyEnter, value);
            }
            remove
            {
                RemoveHandler(TabBuddyEnter, value);
            }
        }

        public event RoutedEventHandler TabBuddyLeaveEvent
        {
            add
            {
                AddHandler(TabBuddyLeave, value);
            }
            remove
            {
                RemoveHandler(TabBuddyLeave, value);
            }
        }


        public override void OnApplyTemplate()
        {
            try
            {
                base.OnApplyTemplate();

                btnClose = base.GetTemplateChild("PART_Close") as Button;

                if (btnClose != null)
                {
                    btnClose.Click += new RoutedEventHandler(btnClose_Click);
                }

                btnRename = base.GetTemplateChild("PART_Rename") as Button;

                if (btnRename != null)
                {
                    btnRename.Click += new RoutedEventHandler(btnRename_Click);
                }

                btnDelete = base.GetTemplateChild("PART_Delete") as Button;

                if (btnDelete != null)
                {
                    btnDelete.Click += new RoutedEventHandler(btnDelete_Click);
                }

                cmbTabBuddy = base.GetTemplateChild("PART_TabBuddy") as ComboBox;

                if (cmbTabBuddy != null)
                {
                    if (lstBuddyDragged != null && lstBuddyDragged.Count > 0)
                    {
                        for (int i = 0; i < lstBuddyDragged.Count; i++)
                        {
                            if(!cmbTabBuddy.Items.Contains(lstBuddyDragged[i]))
                            {
                            ComboItem cbi = new ComboItem();
                            cbi.setName(lstBuddyDragged[i]);
                            cmbTabBuddy.Items.Add(cbi);
                            }
                        }                        
                    }
                    cmbTabBuddy.MouseLeave += new System.Windows.Input.MouseEventHandler(cmbTabBuddy_MouseLeave);
                    cmbTabBuddy.MouseEnter += new System.Windows.Input.MouseEventHandler(cmbTabBuddy_MouseEnter);
                }

            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "OnApplyTemplate()--:--ClsTabsTabItem.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }

        }

        void cmbTabBuddy_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(TabBuddyEnter, this));
        }

        void cmbTabBuddy_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(TabBuddyLeave, this));

        }

        void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.RaiseEvent(new RoutedEventArgs(DeleteTab, this));
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnDelete_Click()--:--ClsTabsTabItem.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void btnRename_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.RaiseEvent(new RoutedEventArgs(RenameTab, this));
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnRename_Click()--:--ClsTabsTabItem.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void btnClose_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.RaiseEvent(new RoutedEventArgs(CloseTab, this));
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnClose_Click()--:--ClsTabsTabItem.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (btnClose != null)
            {
                btnClose = null;
            }
            if (btnDelete != null)
            {
                btnDelete = null;
            }
            if (btnRename != null)
            {
                btnRename = null;
            }
        }

        #endregion

        ~ClsTabsTabItem()
        {
        }

    }
}
